<template>
  <div class="el-card__body">
    <!-- 头部搜索，新增 -->
    <div class="subject-bar">
      <div class="left-form">
        <el-form
          ref="formName"
          :inline="true"
          :model="formName"
          class="demo-form-inline"
        >
          <el-form-item label="学科名称">
            <el-input v-model.trim="formName.subjectName" />
          </el-form-item>

          <el-form-item>
            <el-button @click="resetForm">清除</el-button>

            <el-button type="primary" @click="searchSubject">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="right-form">
        <el-button type="success" @click="addSubject">
          <i class="el-icon-edit" />
          新增学科</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" type="index" />
      <el-table-column prop="subjectName" label="学科名称" />
      <el-table-column prop="username" label="创建者" />
      <el-table-column prop="addDate" label="创建日期">
        <template slot-scope="{ row }">{{ row.addDate |formatDate }}</template>
      </el-table-column>

      <el-table-column prop="isFrontDisplay" label="前台是否显示" />
      <el-table-column prop="twoLevelDirectory" label="二级目录" />
      <el-table-column prop="tags" label="标签" />
      <el-table-column prop="totals" label="题目数量" />
      <el-table-column prop="address" label="操作" width="200">
        <template v-slot="{ row }">
          <el-button type="text" @click="toSubjectDetail(row)">学科分类</el-button>
          <el-button type="text" @click="toTagDetail(row)">学科标签</el-button>
          <el-button type="text" @click="editSubject(row.id)">修改</el-button>
          <el-button type="text" @click="delSubject(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页模块 -->
    <el-row type="flex" justify="end" class="pagerow">
      <el-pagination
        background
        layout="prev, pager, next,sizes,jumper"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pagesize"
        :current-page="page.page"
        :total="page.total"
        @current-change="changePage"
        @size-change="changeSize"
      />
    </el-row>
    <!-- 新增学科弹出层 -->
    <dialogSubject ref="dialogSubject" @updateSubject="getSubjectsList" />
  </div>
</template>
<script>
import { getSubjectsList, delSubjects } from '@/api/subjects/subjects'
import dialogSubject from './components/dialogSubject.vue'
const defaultItem = {
  page: 1,
  pagesize: 5,
  total: 0
}
export default {
  name: 'Subjects',
  components: { dialogSubject },
  props: {},
  data() {
    return {
      loading: false,
      formName: {
        subjectName: ''
      },
      page: {
        ...defaultItem
      },
      tableData: []
      // subjectId:'',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getSubjectsList()
  },
  mounted() {},
  methods: {
    // 获取学科列表
    async getSubjectsList() {
      this.loading = true
      const { data } = await getSubjectsList({
        ...this.page,
        subjectName: this.formName.subjectName
      })
      this.tableData = data.items
      this.page.total = data.counts

      this.loading = false
    },
    // 页码改变时
    changePage(newPage) {
      this.page.page = newPage
      this.getSubjectsList()
    },
    // 每页条数改变时
    changeSize(newSize) {
      this.page.pagesize = newSize
      this.page.page = 1
      this.getSubjectsList()
    },
    // 搜索学科
    async searchSubject() {
      this.page = { ...defaultItem }
      this.getSubjectsList()
    },
    // 重置表单
    resetForm() {
      this.formName.subjectName = ''
      this.getSubjectsList()
    },
    // 新增学科
    addSubject() {
      this.$refs.dialogSubject.showdialogSubject = true
    },
    // 修改学科
    async editSubject(id) {
      //  数据回显
      await this.$refs.dialogSubject.subjectsDetail(id)
      this.$refs.dialogSubject.showdialogSubject = true
    },
    // 删除学科
    async delSubject(id) {
      try {
        await this.$confirm('确认删除该学科吗？')
        await delSubjects(id)
        if (this.tableData.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.$message.success('删除成功')
        this.getSubjectsList()
      } catch (error) {
        console.log(error)
      }
    },
    // 跳转到目录详情页
    toSubjectDetail(row) {
      this.$router.push({ path: '/subjects/directorys', query: { id: row.id, name: row.subjectName }})
    },
    // 跳转到标签详情页
    toTagDetail(row) {
      this.$router.push({ path: '/subjects/tags', query: { id: row.id, name: row.subjectName }})
    }
  }
}
</script>
<style scoped lang="scss">
.subject-bar {
  display: flex;
  justify-content: space-between;
}
.pagerow{
  margin: 15px 0;
}
</style>
